/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.generator.domtree;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * Element decorator
 * 
 * @author Bernard Bou
 */
public class ElementDecorator extends DefaultDecorator
{
	// C O N S T R U C T O R

	/**
	 * Constructor
	 * @param thisNode DOM node
	 */
	public ElementDecorator(Node thisNode)
	{
		super(thisNode);
	}

	// S T R I N G S

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.generator.domtree.DefaultDecorator#toName()
	 */
	@Override
	public String getName()
	{
		return getElement().getNodeName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.generator.domtree.DefaultDecorator#toData()
	 */
	@Override
	public String getValue()
	{
		return getAttributes();
	}

	// A C C E S S

	/**
	 * Get element
	 * 
	 * @return element
	 */
	private Element getElement()
	{
		return (Element) this.theNode;
	}

	// H E L P E R S

	/**
	 * Get element text
	 * 
	 * @return element text
	 */
	protected String getText()
	{
		Node thisChild = theNode.getFirstChild();
		if (thisChild != null)
		{
			return thisChild.getNodeValue();
		}
		return null;
	}

	/**
	 * Get attribute string
	 * 
	 * @return attribute string
	 */
	protected String getAttributes()
	{
		String thisResult = ""; //$NON-NLS-1$
		NamedNodeMap thisMap = getElement().getAttributes();
		if (thisMap != null)
		{
			for (int i = 0; i < thisMap.getLength(); i++)
			{
				if (i != 0)
					thisResult += " "; //$NON-NLS-1$
				Node thisAttribute = thisMap.item(i);
				thisResult += thisAttribute.getNodeName() + "=\"" + thisAttribute.getNodeValue() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		return thisResult;
	}
}
